{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# controlnet + txt2img\n",
    "# enable `Allow other script to control this extension` in settings\n",
    "\n",
    "import requests\n",
    "import cv2\n",
    "from base64 import b64encode\n",
    "\n",
    "def readImage(path):\n",
    "    img = cv2.imread(path)\n",
    "    retval, buffer = cv2.imencode('.jpg', img)\n",
    "    b64img = b64encode(buffer).decode(\"utf-8\")\n",
    "    return b64img\n",
    "\n",
    "b64img = readImage(\"/root/workspace/nahida/0e17302b9bfa15402f783c29c0d1d34f.jpg\")\n",
    "\n",
    "class controlnetRequest():\n",
    "    def __init__(self, prompt):\n",
    "        self.url = \"http://localhost:7860/controlnet/txt2img\"\n",
    "        self.body = {\n",
    "            \"prompt\": prompt,\n",
    "            \"negative_prompt\": \"\",\n",
    "            \"seed\": -1,\n",
    "            \"subseed\": -1,\n",
    "            \"subseed_strength\": 0,\n",
    "            \"batch_size\": 1,\n",
    "            \"n_iter\": 1,\n",
    "            \"steps\": 15,\n",
    "            \"cfg_scale\": 7,\n",
    "            \"width\": 512,\n",
    "            \"height\": 768,\n",
    "            \"restore_faces\": True,\n",
    "            \"eta\": 0,\n",
    "            \"sampler_index\": \"Euler a\",\n",
    "            \"controlnet_input_image\": [b64img],\n",
    "            \"controlnet_module\": 'canny',\n",
    "            \"controlnet_model\": 'control_canny-fp16 [e3fe7712]',\n",
    "            \"controlnet_guidance\": 1.0,\n",
    "        }\n",
    "\n",
    "    def sendRequest(self):\n",
    "        r = requests.post(self.url, json=self.body)\n",
    "        return r.json()\n",
    "\n",
    "js = controlnetRequest(\"walter white\").sendRequest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import io, base64\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "\n",
    "pil_img = Image.open('/root/workspace/nahida/0e17302b9bfa15402f783c29c0d1d34f.jpg')\n",
    "image = Image.open(io.BytesIO(base64.b64decode(js[\"images\"][0])))\n",
    "mask_image = Image.open(io.BytesIO(base64.b64decode(js[\"images\"][1])))\n",
    "\n",
    "plt.figure()\n",
    "f, axarr = plt.subplots(1,3) \n",
    "axarr[0].imshow(pil_img)   \n",
    "axarr[1].imshow(image)  \n",
    "axarr[2].imshow(mask_image)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pynb",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d73345514d8c18d9a1da7351d222dbd2834c7f4a09e728a0d1f4c4580fbec206"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
